//
//  AppDelegate.m
//  PTravel
//
//  Created by dllo on 15/11/15.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "AppDelegate.h"
#import "UMSocial.h"
#import "RootViewController.h"
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIView *blackView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *page;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        RootViewController *root = [[RootViewController alloc] init];
        self.window.rootViewController = root;
        
    }
    else
    {
        [self tabBarSubview];
    }
    sleep(1);
    [_window release];
        return YES;
}

- (void)tabBarSubview
{
    RecommendViewController *recommendVC = [[RecommendViewController alloc] init];
    UINavigationController *recommendNa = [[UINavigationController alloc] initWithRootViewController:recommendVC];
    recommendNa.navigationBar.translucent = NO;
    
    
    DestinationViewController *destinationVC = [[DestinationViewController alloc] init];
    UINavigationController *destinationNa = [[UINavigationController alloc] initWithRootViewController:destinationVC];
    
    
    
    CommunityViewController *communityVC = [[CommunityViewController alloc] init];
    UINavigationController *communityNa = [[UINavigationController alloc] initWithRootViewController:communityVC];
    
    
    MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *mineNa = [[UINavigationController alloc] initWithRootViewController:mineVC];
    self.tabBarC = [[BaseTabBarController alloc] init];
    self.tabBarC.viewControllers = [NSMutableArray arrayWithObjects:recommendNa, destinationNa, communityNa, mineNa, nil];
    self.window.rootViewController = self.tabBarC;
    
    self.tabBarC.delegate = self;
    
    
    [_tabBarC release];
    [mineNa release];
    [mineVC release];
    [communityNa release];
    [communityVC release];
    [destinationVC release];
    [destinationNa release];
    [recommendNa release];
    [recommendVC release];
    

}

#pragma  mark ** 引导页
- (void)subViews
{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    [self.window addSubview:self.scrollView];
    [self.scrollView release];
    self.scrollView.contentSize = CGSizeMake(self.window.frame.size.width * 3, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
    
    for (int i = 0; i < 3; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:
                                  CGRectMake(self.window.frame.size.width * i, 0, self.window.frame.size.width, self.window.frame.size.height)];
        [self.scrollView addSubview:imageView];
        
        [imageView release];
        
        imageView.tag = 400 + i;
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"about%d.png",i]];
        
        if(i == 0)
        {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:view];
            [view release];
            view.backgroundColor = [UIColor grayColor];
            view.alpha = 0.3;
            
            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:title];
            [title release];
            title.text = @"热门推荐,来一场说走就走的旅行";
            title.textColor = [UIColor whiteColor];
            title.font = [UIFont boldSystemFontOfSize:20];
            title.textAlignment = NSTextAlignmentCenter;
        }
        
        if (i == 1) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:view];
            [view release];
            view.backgroundColor = [UIColor grayColor];
            view.alpha = 0.3;
            
            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:title];
            [title release];
            title.text = @"更多目的地,想去哪里就去哪里";
            title.textColor = [UIColor whiteColor];
            title.font = [UIFont boldSystemFontOfSize:20];
            title.textAlignment = NSTextAlignmentCenter;
        }
        
        if (i == 2) {
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:view];
            [view release];
            view.backgroundColor = [UIColor grayColor];
            view.alpha = 0.3;
            
            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.window.frame.size.width - 20, 30)];
            [imageView addSubview:title];
            [title release];
            title.text = @"人在旅途,看看别人怎么玩";
            title.textColor = [UIColor whiteColor];
            title.font = [UIFont boldSystemFontOfSize:20];
            title.textAlignment = NSTextAlignmentCenter;
            
            UIButton *begin = [UIButton buttonWithType:UIButtonTypeCustom];
            begin.frame = CGRectMake(0, 0, 100, 40);
            begin.center = CGPointMake(self.window.center.x, self.window.frame.size.height - 120);
            [imageView addSubview:begin];
            imageView.userInteractionEnabled = YES;
            
            [begin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [begin setTitle:@"开始旅途" forState:UIControlStateNormal];
            [begin addTarget:self action:@selector(begin) forControlEvents:UIControlEventTouchUpInside];
            begin.backgroundColor = [UIColor colorWithRed:108 / 255.0 green:108 / 255.0 blue:108 / 255.0 alpha:0.5];
        }
    }
    
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
    self.page.backgroundColor = [UIColor colorWithRed:108 / 255.0 green:108 / 255.0 blue:108 / 255.0 alpha:0.4];
    [self.window addSubview:self.page];
    [self.page release];
    self.page.center = CGPointMake(self.window.center.x, self.window.frame.size.height - 50);
    self.page.numberOfPages = 3;
    
    
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
